<?php
/*
* Copyright (c) 2010 by Cediti
*
* You may distribute under the terms of either the GNU Lesser General
* Public License or the new BSD License, as specified in the README file.
*/
// created :    09/02/2010
// version :    21/04/2010

class Cmis_Client_Rest_Generic_Object extends Cmis_Client_Rest_Generic_Abstract implements Cmis_Services_Protocol_Object
{
	public function createDocument(Cmis_Repository_Id $repositoryId, Cmis_Repository_Properties $properties, Cmis_Repository_Id $folderId = null, array $contentStream = null, $versioningState = 'major', array $policies = null, array $addACEs = null, array $removeACEs = null)
	{
		//check repository
		$cur = $this->_getRepositoryInfo($repositoryId);
		$links = $cur->getCollection('root');

		if (!empty($folderId) && !$folderId->isEmpty())
		{
			$folder = $this->getServices()->getService('Object')->getObject($repositoryId, $folderId, null, null, false, null, false, true);
			$links = $folder->getLinks('down', self::FEED_TYPE_ATOM_FEED);
		}

		if (empty($links) || $links['multiple'] || ($links['type'] != self::FEED_TYPE_ATOM_FEED && $links['type'] != 'root'))
		{
			throw new Cmis_Exception_Services_Inaccessible('The down link of post action to createDocument was not found');
		}

		$this->getProtocolClient()->setCheckReturnType(true);
		$this->getProtocolClient()->doPost($links['href'], $contentStream[2], self::FEED_TYPE_ATOM_ENTRY);
		return $this->getProtocolClient()->getResponse();
	}

	public function createFolder(Cmis_Repository_Id $repositoryId, Cmis_Repository_Properties $properties, Cmis_Repository_Id $folderId, array $policies = null, array $addACEs = null, array $removeACEs = null)
	{
        //check repository
        $cur = $this->_getRepositoryInfo($repositoryId);
        $links = $cur->getCollection('root');

        if (!empty($folderId) && !$folderId->isEmpty())
        {
            $folder = $this->getServices()->getService('Object')->getObject($repositoryId, $folderId);
            $links = $folder->getLinks('down', self::FEED_TYPE_ATOM_FEED);
        }

        if (empty($links) || $links['multiple'] || ($links['type'] != self::FEED_TYPE_ATOM_FEED && $links['type'] != 'root'))
        {
            throw new Cmis_Exception_Services_Inaccessible('The down link of post action to createFolder was not found');
        }

        $num = func_num_args();
        $xml = ($num >= 6)?func_get_arg(6):null;
        if (is_null($xml))
        {
        	throw new Cmis_Excepion('Can\'t make post, data is missing');
        }

        $this->getProtocolClient()->setCheckReturnType(true);
        $this->getProtocolClient()->doPost($links['href'], $xml, self::FEED_TYPE_ATOM_ENTRY);
        return $this->getProtocolClient()->getResponse();
	}

	public function createRelationship(Cmis_Repository_Id $repositoryId, $typeId, $properties, $sourceObjectId, $targetObjectId)
	{
		die('-[Must be implemented : ' . __METHOD__ . ' (' . __FILE__ . '#' . __LINE__ . ')]-');
	}

	public function createPolicy(Cmis_Repository_Id $repositoryId, $typeId, $properties, $folderId)
	{
		die('-[Must be implemented : ' . __METHOD__ . ' (' . __FILE__ . '#' . __LINE__ . ')]-');
	}

	public function getAllowableActions(Cmis_Repository_Id $repositoryId, Cmis_Repository_Id $objectId)
	{
		$object = $this->getServices()->getService('Object')->getObject($repositoryId, $folderId);
        $links = $folder->getLinks('http://docs.oasis-open.org/ns/cmis/link/200908/allowableactions');

        return $this->_callServices($links['href']);
	}

	public function getProperties(Cmis_Repository_Id $repositoryId, Cmis_Repository_Id $objectId, $filter = null)
	{
		return $this->getObject($repositoryId, $objectId, $filter);
	}

	public function getContentStream(Cmis_Repository_Id $repositoryId, Cmis_Repository_Id $objectId, $streamId = null)
	{
		//enclosure link relation do the same ...
		/* @var $object Cmis_Repository_Entry */
		$content = $this->getServices()->getService('Object')->getObject($repositoryId, $objectId)->getInfo('content');

		$this->getProtocolClient()
			->setCheckReturnType(false)
			->doGet($content['src']);
		return $this->getProtocolClient()->getResponse();
	}

	public function updateProperties(Cmis_Repository_Id $repositoryId, Cmis_Repository_Id $objectId, $changeToken, $propertyCollection)
	{
		//Changes
		die('-[Must be implemented : ' . __METHOD__ . ' (' . __FILE__ . '#' . __LINE__ . ')]-');
	}

	public function moveObject(Cmis_Repository_Id $repositoryId, Cmis_Repository_Id $objectId, $targetFolderId, $sourceFolderId)
	{
		die('-[Must be implemented : ' . __METHOD__ . ' (' . __FILE__ . '#' . __LINE__ . ')]-');
	}

	public function deleteObject(Cmis_Repository_Id $repositoryId, Cmis_Repository_Id $objectId, $allVersions = true)
	{
		$object = $this->getServices()->getService('Object')->getObject($repositoryId, $objectId);
		$link = $object->getLinks('self');

		$url = Cmis_ServiceUtils::buildUrl($link['href'], array('allVersions' => $allVersions));

		$this->getProtocolClient()
            ->setCheckReturnType(true)
            ->doDelete($url);
        return $this->getProtocolClient()->getResponse();
	}

	public function deteleTree(Cmis_Repository_Id $repositoryId, Cmis_Repository_Id $folderId, $unfileNonfolderObjects)
	{
		die('-[Must be implemented : ' . __METHOD__ . ' (' . __FILE__ . '#' . __LINE__ . ')]-');
	}

	public function setContentStream(Cmis_Repository_Id $repositoryId, Cmis_Repository_Id $objectId, $overwriteFlag, $content, $properties)
	{
		/*$cur = $this->_getRepositoryInfo($repositoryId);
		 switch($cur->getCapapbilities('ContentStreamUpdatability'))
		 {
		 case Cmis_Repository_Capability_ContentStreamUpdatability::VALUE_NONE :
		 throw new Cmis_Exception_Services_NoCapability('create');
		 case Cmis_Repository_Capability_ContentStreamUpdatability::VALUE_ANYTIME :
		 case Cmis_Repository_Capability_ContentStreamUpdatability::VALUE_PWCONLY :
		 }*/
		die('-[Must be implemented : ' . __METHOD__ . ' (' . __FILE__ . '#' . __LINE__ . ')]-');
	}

	public function deleteContentStream(Cmis_Repository_Id $repositoryId, Cmis_Repository_Id $objectId)
	{
		die('-[Must be implemented : ' . __METHOD__ . ' (' . __FILE__ . '#' . __LINE__ . ')]-');
	}

	public function getObject(Cmis_Repository_Id $repositoryId, Cmis_Repository_Id $objectId, $filter = null, $includeRelationships = null, $includePolicyIds = false, $renditionFilter = null, $includeACL = false, $includeAllowableActions = false)
	{
		$cur = $this->_getRepositoryInfo($repositoryId);

		$objectById = $cur->getTemplate('objectbyid');
		$params = array(
            'id'  => $objectId->getId()
		);
		return $this->_getObjectByTemplate($objectById['template'], $params, $repositoryId, $filter, $includeAllowableActions, $includeRelationships, $renditionFilter, $includePolicyIds, $includeACL);
	}

	public function getObjectByPath(Cmis_Repository_Id $repositoryId, $path, $filter = null, $includeAllowableActions = false, $includeRelationships = null, $renditionFilter = null, $includePolicyIds = false, $includeACL = false)
	{
		$cur = $this->_getRepositoryInfo($repositoryId);

		$objectByPath = $cur->getTemplate('objectbypath');
		$params = array(
            'path'  => $path
		);
		return $this->_getObjectByTemplate($objectByPath['template'], $params, $repositoryId, $filter, $includeAllowableActions, $includeRelationships, $renditionFilter, $includePolicyIds, $includeACL);
	}

	protected function _getObjectByTemplate($template, array $params, Cmis_Repository_Id $repositoryId, $filter, $includeAllowableActions, $includeRelationships, $renditionFilter, $includePolicyIds, $includeACL)
	{
		$param = array(
            'repositoryId'  => $repositoryId,
            'filter'    => $filter,
            'includeAllowableActions'   => $includeAllowableActions,
            'includeRelationships'  => $includeRelationships,
            'renditionFilter'   => $renditionFilter,
            'includePolicyIds'  => $includePolicyIds,
            'includeACL'    => $includeACL,
		);
		$params = $params + $param;
		$url = Cmis_ServiceUtils::urlTemplateParse($template, $params);
		try
		{
			return $this->_callServices($url);
		}
		catch(Cmis_Exception_Services_Services_NotFound $e)
		{
			throw new Cmis_Exception_Services_Object_NotFound($e);
		}
	}
}

# EOF
